package descentFly.away3D4.action
{
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	
	import descentFly.away3D4.df_path;
	import descentFly.away3D4.df_pathFromMesh;
	
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;

	public class df_moving extends df_OPAction {
		private var _path			: df_path;
		public function df_moving(a_options: Object, a_model: ObjectContainer3D) {
			super(a_options, a_model);
			initPath(options.path);
		}
		
		protected function initPath(a_path: Object): void {
			if (a_path is Vector.<Number>)
				_path = new df_path(a_path as Vector.<Number>, new Matrix3D());
			else if (a_path is String)
				_path = new df_pathFromMesh(findFromName(a_path as String) as Mesh);
		}
		
		override protected function update(percent: Number): void {
			applyCurrentToObjects(_path.getDataFromPercent(percent, options.cycle));
		}
		
		protected function applyCurrentToObjects(current: Object): void {
			objects.forEach(function (object: ObjectContainer3D, i: int, arr: Array): void {
				applyCurrent(originMatrix[i].clone(), object, current);
			});
		}
		
		protected function applyCurrent(originMatrix: Matrix3D, object: ObjectContainer3D, current: Object): void {
			var mat: Matrix3D = originMatrix;
			mat.appendTranslation(current.position.x, current.position.y, current.position.z);
			object.transform = mat;
		}
	}
}